package com.sens.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sens.orderback.R;
import com.sens.tables.MybankOrder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MybankOrderAdapter extends BaseAdapter {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private List<MybankOrder> list = new ArrayList<>(10);

    public void addOrder(MybankOrder... alipayOrders) {
        Collections.addAll(list, alipayOrders);
    }

    public void addOrder(Collection<MybankOrder> alipayOrders) {
        list.addAll(alipayOrders);
    }

    public void clear() {
        list.clear();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MybankOrder getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mybank_order, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.updateOrder(getItem(position));
        return convertView;
    }

    class ViewHolder {
        private TextView tradeNoTV;
        private TextView moneyTV;
        private TextView timeTV;
        private TextView hasUpload;

        ViewHolder(View view) {
            tradeNoTV = view.findViewById(R.id.tradeNoTV);
            moneyTV = view.findViewById(R.id.moneyTV);
            timeTV = view.findViewById(R.id.timeTV);
            hasUpload = view.findViewById(R.id.hasUpload);
        }

        void updateOrder(MybankOrder mybankOrder) {
            tradeNoTV.setText("单号：" + mybankOrder.getOrderId());
            moneyTV.setText(String.valueOf(mybankOrder.getAmount()));
            timeTV.setText("时间："+simpleDateFormat.format(new Date(mybankOrder.getTime())));
            if (mybankOrder.isHasUpload()) {
                hasUpload.setTextColor(0xFF0099CC);
                hasUpload.setText("已上传");
            } else {
                hasUpload.setTextColor(0xFFCC0000);
                hasUpload.setText("未上传");
            }
        }
    }
}
